import {ipcMain, dialog, BrowserWindow, app, WebContents} from "electron";
import {IsUseSysTitle} from "../config/const";
import Server from "../server";
import WsServer from "../server/wsServer";
import {winURL, preloadURL, staticPaths} from "../config/staticPath";
import {updater} from "./HotUpdater";
import {updater as updaterTest} from "./HotUpdaterTest";
import DownloadFile from "./downloadFile";
import Update from "./checkUpdate";
import {otherWindowConfig} from "../config/windowsConfig";
import {usePrintHandle} from "src/main/handle/printHandle";
import {useBrowserHandle} from "src/main/handle/browserHandle";
import {useMainHandle} from "src/main/handle/mainHandle";
import {UpdateStatus} from "electron_updater_node_core";
import {IpcMainHandle, IpcChannel, winContentSend} from "../ipc";
import {ProgressInfo} from "electron-updater";
import {showOnMyComputer, hideOnMyComputer, checkIsShowOnMyComputer} from "./regeditUtils"
import {openDevTools} from "../hook/devToolHook";

const ALL_UPDATER = new Update();

const ipcMainHandle: IpcMainHandle = {
	[IpcChannel.IsUseSysTitle]: () => {
		return IsUseSysTitle;
	},

	[IpcChannel.WindowMini]: (event) => {
		BrowserWindow.fromWebContents(event.sender)?.minimize();
	},
	[IpcChannel.WindowMax]: (event) => {
		if (BrowserWindow.fromWebContents(event.sender)?.isMaximized()) {
			BrowserWindow.fromWebContents(event.sender)?.restore();
			return {status: false};
		} else {
			BrowserWindow.fromWebContents(event.sender)?.maximize();
			return {status: true};
		}
	},
	[IpcChannel.WindowClose]: (event) => {
		BrowserWindow.fromWebContents(event.sender)?.close();
	},

	[IpcChannel.CheckUpdate]: (event) => {
		ALL_UPDATER.checkUpdate(BrowserWindow.fromWebContents(event.sender));
	},
	[IpcChannel.ConfirmUpdate]: () => {
		ALL_UPDATER.quitAndInstall();
	},
	[IpcChannel.AppClose]: () => {
		//关闭APP前先停止内置服务器 By fm453
		// Server.StopServer();
		//
		app.quit();
	},
	[IpcChannel.GetStaticPath]: () => {
		return staticPaths;
	},
	[IpcChannel.OpenMessagebox]: async (event, arg) => {
		return dialog.showMessageBox(
			BrowserWindow.fromWebContents(event.sender),
			{
				type: arg.type || "info",
				title: arg.title || "",
				buttons: arg.buttons || [],
				message: arg.message || "",
				noLink: arg.noLink || true,
			}
		);
	},

	[IpcChannel.OpenErrorbox]: (_event, arg) => {
		dialog.showErrorBox(arg.title, arg.message)
	},
	[IpcChannel.StartServer]: async () => {
		try {
			const serverStatus = await Server.StartServer();
			return serverStatus;
		} catch (error) {
			dialog.showErrorBox("错误", error);
			return ""
		}
	},
	[IpcChannel.StopServer]: async () => {
		try {
			const serverStatus = await Server.StopServer();
			return serverStatus;
		} catch (error) {
			dialog.showErrorBox("错误", error);
			return ""
		}
	},
	[IpcChannel.StartWsServer]: async () => {
		try {
			const serverStatus = await WsServer.StartServer();
			return serverStatus;
		} catch (error) {
			dialog.showErrorBox("错误", error);
			return ""
		}
	},
	[IpcChannel.StopWsServer]: async () => {
		try {
			const serverStatus = await WsServer.StopServer();
			return serverStatus;
		} catch (error) {
			dialog.showErrorBox("错误", error);
			return ""
		}
	},

	[IpcChannel.HotUpdate]: (event) => {
		updater(BrowserWindow.fromWebContents(event.sender))
	},
	[IpcChannel.HotUpdateTest]: async (event, arg) => {
		console.log("hot-update-test");
		try {
			let updateInfo = await updaterTest(
				BrowserWindow.fromWebContents(event.sender)
			);
			if (updateInfo === UpdateStatus.Success) {
				app.quit();
			} else if (updateInfo === UpdateStatus.HaveNothingUpdate) {
				console.log("不需要更新");
			} else if (updateInfo === UpdateStatus.Failed) {
				console.error("更新出错");
			}
		} catch (error) {
			// 更新出错
			console.error("更新出错");
		}
	},
	[IpcChannel.StartDownload]: (event, downloadUrl) => {
		new DownloadFile(
			BrowserWindow.fromWebContents(event.sender),
			downloadUrl
		).start();
	},
	[IpcChannel.OpenWin]: (_event, arg) => {
		const ChildWin = new BrowserWindow({
			titleBarStyle: IsUseSysTitle ? "default" : "hidden",
			...Object.assign(otherWindowConfig, {}),
		});
		// 开发模式下自动开启devtools
		if (process.env.NODE_ENV === "development") {
			openDevTools(ChildWin)
		}
		ChildWin.loadURL(winURL + `#${arg.url}`);
		ChildWin.once("ready-to-show", () => {
			ChildWin.show();
			if (arg.IsPay) {
				// 检查支付时候自动关闭小窗口
				const testUrl = setInterval(() => {
					const Url = ChildWin.webContents.getURL();
					if (Url.includes(arg.PayUrl)) {
						ChildWin.close();
					}
				}, 1200);
				ChildWin.on("close", () => {
					clearInterval(testUrl);
				});
			}
		});
		// 渲染进程显示时触发
		ChildWin.once("show", () => {
			ChildWin.webContents.send("send-data-test", arg.sendData);
		});
	},
	[IpcChannel.CheckShowOnMyComputer]: async () => {
		return await checkIsShowOnMyComputer()
	},
	[IpcChannel.SetShowOnMyComputer]: async (event, bool) => {
		if (bool) {
			return await showOnMyComputer()
		} else {
			return await hideOnMyComputer()
		}
	},
	...usePrintHandle(),
	...useBrowserHandle(),
}

export function installIpcMain() {
	Object.entries(ipcMainHandle).forEach(([ipcChannelName, ipcListener]) => {
		ipcMain.handle(ipcChannelName, ipcListener)
	})
}
